package com.pwx.student.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.pwx.student.common.ResponseResult;
import com.pwx.student.dao.RcCourseMapper;
import com.pwx.student.entity.RcCourse;
import com.pwx.student.entity.RcStudentCourse;
import com.pwx.student.dao.RcStudentCourseMapper;
import com.pwx.student.service.IRcStudentCourseService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pwx.student.vo.output.CourseTimeOutputVo;
import com.pwx.student.vo.output.StudentCourseVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author saber
 * @since 2022-10-24
 */
@Service
public class RcStudentCourseServiceImpl extends ServiceImpl<RcStudentCourseMapper, RcStudentCourse> implements IRcStudentCourseService {

    @Autowired
    private RcStudentCourseMapper rcStudentCourseMapper;

    @Autowired
    private RcCourseMapper rcCourseMapper;

    @Override
    public ResponseResult showStudentCourse(Integer studentId) {
        List<StudentCourseVo> studentCourseVo = rcStudentCourseMapper.showStudentCourse(studentId);
        if (studentCourseVo!=null){
            return ResponseResult.createSuccess(studentCourseVo);
        }
        return ResponseResult.createFail("没有查询到学生数据");
    }

    @Override
    public ResponseResult changeCourse(Integer scId, Integer studentId) {
        RcStudentCourse rcStudentCourse = rcStudentCourseMapper.selectOne(new QueryWrapper<RcStudentCourse>()
                .eq("sc_id", scId).eq("sc_student_id", studentId));
        if (rcStudentCourse==null){
            return ResponseResult.createFail("没有选修该课程");
        }
        RcCourse course = rcCourseMapper.selectOne(new QueryWrapper<RcCourse>()
                .eq("course_id", rcStudentCourse.getScCourseId()));
        if (course==null){
            return ResponseResult.createFail("没有该课程");
        }
        course.setCourseSelectedCount(course.getCourseSelectedCount()-1);
        rcCourseMapper.updateById(course);
        int delete = rcStudentCourseMapper.delete(new QueryWrapper<RcStudentCourse>()
                .eq("sc_id",scId));
        if (delete>0){
            return ResponseResult.createSuccess("退选课程成功");
        }

        return ResponseResult.createFail("退选课程失败");
    }

    @Override
    public ResponseResult studentCourseTime(Integer studentId) {
        List<CourseTimeOutputVo> voList = rcStudentCourseMapper.studentCourseTime(studentId);
        if (voList!=null){
            return ResponseResult.createSuccess(voList);
        }
        return ResponseResult.createFail("没有查询到学生课表数据");
    }

    @Override
    public ResponseResult studentScore(Integer studentId) {
        List<StudentCourseVo> studentCourseVo = rcStudentCourseMapper.studentScore(studentId);
        if (studentCourseVo!=null){
            return ResponseResult.createSuccess(studentCourseVo);
        }
        return ResponseResult.createFail("没有查询到学生分数数据");
    }
}
